import O1HD from "../../engines/gameEngine";
import CsvDataManage from "../../engines/managers/CsvDataManage";
import { peopleArcheryRole } from "../../engines/managers/peopleArcheryManage";
import mainRoot from "../MainRoot";

const {ccclass, property} = cc._decorator;

@ccclass
export default class peopleArcheryRoleItem extends cc.Component {

    @property({type:cc.Node,tooltip:"显示节点"})
    showNode:cc.Node = null;

    @property({type:cc.Label,tooltip:"名字文本"})
    lbName:cc.Label = null;

    @property({type:cc.Label,tooltip:"等级文本"})
    lbLevel:cc.Label = null;

    data:any = null;

    start(){
        O1HD.on("peopleArcheryRoleItem.refrensh",this.refenshItem,this);
    }

    bindData(data:any){
        this.data = data;
        this.refenshItem();
    }

    refenshItem(){
        if(!this.data)return;
        this.lbName.string = this.data.role_name;
        let id = this.data.role_id;
        let saveData = O1HD.getManagers().peopleArcheryManager.getRoleData(id);
        if(saveData){
            let level:number = saveData.level;
            this.lbLevel.string = "lv."+level.toString();
            this.setIcon(true);
        }else{
            this.lbLevel.string = "未解锁";
            this.setIcon(false);
        }
    }

    setIcon(unlockFlag:boolean){
        let iconPath:string = this.data["role_path"] + (unlockFlag ? "" : "_gray");
        mainRoot.frameBundle.load(`Img/Page/selectRole/${iconPath}`,cc.SpriteFrame,(err,spf:cc.SpriteFrame)=>{
            if(err){
                console.log(err,"加载icon失败");
                return;
            }
            this.showNode.getComponent(cc.Sprite).spriteFrame = spf;
        });
    }
}
